/*
* Description: 负载探测
*/
#ifndef PAYLOAD_SENSOR_H
#define PAYLOAD_SENSOR_H

#include <memory>
#include "singleton.h"
#include "rpc_service/sendto_host.h"

namespace GUEST {
class PayloadSensor {
public:
    std::shared_ptr<WINCONTROL::SendToHost> GetSendToHost();
    void Init(std::shared_ptr<WINCONTROL::SendToHost> sendToHostSharedPtr);
    void Quit();
    bool GetIsEnableCpuUpdate();
    void SetIsIncreasedFreq(const bool isIncreased);
    bool GetIsIncreasedFreq();
    void CompressDecompressApplicationFilter(unsigned long pid, std::string& name);
    void CompressDecompressApplicationDeFilter(unsigned long pid);
private:
    void LoadSysCalThread();
    void SetIsEnableCpuUpdate(const bool enableOrDisable);
    void EnableIncreaseFrequency();
    std::shared_ptr<WINCONTROL::SendToHost> sendToHost = nullptr;
    // CPU使用率更新时间间隔，单位:ms
    std::int32_t cpuUpdateSleepTime = 1000;
    std::atomic<bool> isEnableCpuUpdate{ true };
    std::thread sysCpuCalThread;
    std::atomic<bool> isIncreasedFreq{ false };
    std::map<unsigned long, std::string> filterMap_ = { };
    std::unordered_set<std::string> appWhiteList = {
        "Bandizip.exe",
        "WinRAR.exe",
        "7zG.exe",
        "acad.exe"
    };
    std::atomic<bool> isAllowedRun_{ false };
    std::condition_variable cv_;
    std::mutex mtx_;
    bool IsIncreaseFreqOverFoolProolTime();
    int64_t GetSystemCurrentTimeInMilli();
    void InvokeIncreaseFreqFunction();
    void InvokeDecreaseFreqFunction();
    // Fool proof operation time in millisecond
    int64_t foolProolOperationTime_ = 125 * 1000;
    int64_t startTimestamp_ = 0;
    std::atomic<bool> isInQuitProcess_{ false };
};
#define PAYLOAD_SENSOR SingletonNew<GUEST::PayloadSensor>::GetInstance()
} // namespace GUEST
#endif